package com.cys.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cys.bean.WaterQCData;
import com.cys.db.DBHelper;

/**
 * Servlet implementation class WaterQCDataServlet
 */
@WebServlet("/WaterQCDataServlet")
public class WaterQCDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WaterQCDataServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			// Connection conn = DriverManager.getConnection(url, username,
			// password);
			Connection conn = DBHelper.getConn();
			// System.out.println(conn);
			// 获取Statement
			Statement stmt = conn.createStatement();
			// 添加图书信息的SQL语句
			String sql = "select * from t_data_dev_water_controlor order by data_time limit 1";
			// 执行查询
			ResultSet rs = stmt.executeQuery(sql);
			// System.out.println(rs);
			// 实例化List对象
			List<WaterQCData> wqlist = new ArrayList<WaterQCData>();
			// 判断光标向后移动，并判断是否有效
			while (rs.next()) {
				// 实例化Book对象
			//	WaterData waterData=new WaterData();
				WaterQCData waterQCData =new WaterQCData();
				waterQCData.setDev_water_controlor(rs.getInt("dev_water_controlor"));
				waterQCData.setData_time(rs.getString("data_time"));
				waterQCData.setLevel(rs.getFloat("level"));
				waterQCData.setMode(rs.getInt("mode"));
				waterQCData.setState(rs.getInt("state"));
				waterQCData.setAlarm_leveldown(rs.getInt("alarm_leveldown"));
				waterQCData.setAlarm_levelup(rs.getInt("alarm_levelup"));
				waterQCData.setAlarm_startup(rs.getInt("alarm_startup"));
				waterQCData.setAlarm_stop(rs.getInt("alarm_stop"));
				wqlist.add(waterQCData);
				//System.out.println(wlist.size());
			}
			// 将图书集合放置到request之中
			request.setAttribute("wqlist", wqlist);
			rs.close(); // 关闭ResultSet
			stmt.close(); // 关闭Statement
			conn.close(); // 关闭Connection
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 请求转发到book_list.jsp
		request.getRequestDispatcher("LiveData.jsp").forward(request, response);
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
